<!--
  -    Copyright (c) 2018-2025, cloud All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: cloud
  -->
<template>
  <BasicContainer>
    <!-- 点击行事件方法  @row-click="handleRowClick"-->
    <!-- 鼠标退出单元格事件 @cell-mouse-leave="cellMouseLeave" -->
    <FyCurd
      ref="crud"
      v-model="form"
      :page.sync="page"
      :data="tableData"
      :permission="permissionList"
      :table-loading="tableLoading"
      :option="tableOption"
      @row-click="handleRowClick"
      @on-load="getList"
      @search-change="searchChange"
      @refresh-change="refreshChange"
      @size-change="sizeChange"
      @current-change="currentChange"
      @row-update="handleUpdate"
      @row-save="handleSave"
      @row-del="handleDel">
      <template #menuLeft>
        <ElButton
          v-if="permissions.purchaseApplyList_fyassetpurchaseapplylist_export"
          class="filter-item"
          plain
          type="primary"
          size="small"
          icon="el-icon-download"
          @click="exportExcel">
          导出
        </ElButton>
        <ElButton type="">申请明细-物品列表</ElButton>
      </template>
      <template #menuRight="{ size }">
        <ElButton v-if="sauInsetBut" type="primary" icon="el-icon-edit" :size="size" @click="openSau">添加</ElButton>
        <ElButton v-if="sauInsetBut" type="primary" :size="size" @click="openPurchase">从其他申请单复制</ElButton>
      </template>
      <template #menu="{ row, index }">
        <ElButton type="text" size="small" @click="rowCell(row, index)">
          {{ row.$cellEdit ? '确定' : '修改' }}
        </ElButton>
      </template>
      <template #price="{ row }">
        <ElTag>{{ row.price }}</ElTag>
      </template>
      <template #priceForm="{ row, disabled }">
        <ElInputNumber v-model="row.price" :disabled="disabled" @change="setAmount(row)" />
      </template>
      <template #quantity="{ row }">
        <ElTag>{{ row.quantity }}</ElTag>
      </template>
      <template #quantityForm="{ row, disabled }">
        <ElInputNumber v-model="row.quantity" :disabled="disabled" @change="setAmount(row)" />
      </template>
      <template #amount="{ row }">
        <ElTag>{{ row.amount }}</ElTag>
      </template>
      <template #amountForm="{ row, disabled }">
        <ElInputNumber v-model="row.amount" :disabled="disabled" />
      </template>
    </FyCurd>
  </BasicContainer>
</template>

<script>
  import { delObj, getPage } from '@/api/asset/fyassetpurchaseapplylist'
  import { tableOption } from '@/const/crud/asset/fyassetpurchaseapplylist'
  import { services } from '@/config'
  import { mapState } from 'vuex'

  export default {
    name: 'Assetpurchaseapplylist',
    components: {},
    props: {
      batchNumber: {
        type: String
      },
      viewType: {
        type: String
      }
    },
    data() {
      return {
        searchForm: {},
        tableData: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20 // 每页显示多少条
        },
        tableLoading: false,
        tableOption,
        sauInsetBut: true,
        form: {},
        parentType: ''
      }
    },
    computed: {
      ...mapState('user', ['permissions']),
      permissionList() {
        return {
          addBtn: this.$isEmpty(this.permissions.purchaseApplyList_fyassetpurchaseapplylist_add, false),
          delBtn: this.$isEmpty(this.permissions.purchaseApplyList_fyassetpurchaseapplylist_del, false),
          editBtn: this.$isEmpty(this.permissions.purchaseApplyList_fyassetpurchaseapplylist_edit, false)
        }
      }
    },
    created() {
      this.init()
    },
    methods: {
      init() {
        // 标识当前父类型是什么类型
        this.parentType = window.boxType
        // 如果当前主单状态为新增或修改 就隐藏一下列
        if ((this.parentType === 'add' || this.parentType === 'edit') && this.viewType !== 'check') {
          tableOption.column[0].hide = true // 状态
          tableOption.column[12].hide = true // 已采购数量
          tableOption.column[14].hide = true // 已到货数量
          tableOption.menu = true // 菜单
        } else {
          tableOption.column[0].hide = false // 状态
          tableOption.column[12].hide = false // 已采购数量
          tableOption.column[14].hide = false // 已到货数量
          tableOption.menu = false // 菜单
          this.sauInsetBut = false
        }
      },
      // 列表查询
      getList(page, params) {
        if (this.parentType === 'edit') {
          this.tableLoading = true
          // 根据批次号查询
          this.searchForm.batchnumber = this.batchNumber
          getPage(
            Object.assign(
              {
                current: page.currentPage,
                size: page.pageSize
              },
              params,
              this.searchForm
            )
          )
            .then(response => {
              this.tableData = response.data.data.records
              this.page.total = response.data.data.total
              this.tableLoading = false
            })
            .catch(() => {
              this.tableLoading = false
            })
        }
      },
      // 删除
      handleDel: function (row, index) {
        this.$confirm(`是否确认删除ID为${row.id}`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            return delObj(row.id)
          })
          .then(data => {
            this.$message.success('删除成功')
            this.getList(this.page)
          })
      },
      rowCell(row, index) {
        // console.log(row, index)
        this.$refs.crud.rowCell(row, index)
      },
      // 更新
      handleUpdate: function (row, index, done, loading) {
        this.$message.success('修改成功')
        done()
        // this.getList(this.page)
      },
      // 保存
      handleSave: function (row, done, loading) {
        // row.batchNumber=this.batchNumber
        // addChildList(row).then(data => {
        this.$message.success('添加成功')
        done()
        // this.getList(this.page)
        // }).catch(() => {
        //   loading()
        // })
      },
      // 每页条数改变事件
      sizeChange(pageSize) {
        this.page.pageSize = pageSize
      },
      // 当前页发生改变事件
      currentChange(current) {
        this.page.currentPage = current
      },
      // 查询事件
      searchChange(form, done) {
        this.searchForm = form
        this.page.currentPage = 1
        this.getList(this.page, form)
        done()
      },
      // 刷新事件
      refreshChange() {
        this.getList(this.page)
      },
      //  导出excel
      exportExcel() {
        this.$downBlobFile(
          `${services.assetService}fyassetpurchaseapplylist/export`,
          this.searchForm,
          'fyassetpurchaseapplylist.xlsx'
        )
      },
      openSau() {
        // openSau: 是父组件指定的传数据绑定的函数，可以再后面传递参数
        this.$emit('openSau')
      },
      // 打开主单
      openPurchase() {
        this.$message.success('点击成功')
        this.$emit('openPurchaseApply')
      },
      handleRowClick(row, event, column) {
        this.tableData[row.$index].amount = parseFloat(row.price) * parseFloat(row.quantity)
        // console.log(row)
      },
      // cellMouseLeave(row,column,cell,event){
      //   // TODO 当鼠标退出单价和采购数量时执行,主要目的是刷新数据
      //   if ((column.property === 'price' || column.property === 'quantity') && row.$cellEdit !=false){
      //     row.$cellEdit=false
      //     this.$refs.crud.rowCell(row, row.$index)
      //   }
      // },
      setAmount(row) {
        row.amount = parseFloat(row.price) * parseFloat(row.quantity)
      }
    }
  }
</script>
